<template>
  <div id="VideoWorkTopSearch">
    <div class="search-box">
      <el-form
        ref="ruleForm"
        :inline="true"
        :model="formInline"
        :rules="rules"
        class="demo-form-inline"
      >
        <el-form-item label="列表排序" prop="sortRule">
          <el-select v-model="formInline.sortRule" placeholder="">
            <el-option
              v-for="item in sortList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="删除状态" prop="isDel">
          <el-select v-model="formInline.isDel" placeholder="">
            <el-option
              v-for="item in delStatusList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择学院" prop="categoryId">
          <el-select v-model="formInline.categoryId" placeholder="">
            <el-option
              v-for="item in collegeList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="timeType">
          <el-select v-model="formInline.timeType" placeholder="">
            <el-option
              v-for="item in timeTypeList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="createTime">
          <el-date-picker
            v-model="formInline.createTime"
            class="datetimerange"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否观看" prop="isWatch">
          <el-select v-model="formInline.isWatch" placeholder="">
            <el-option
              v-for="item in watchList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户信息" prop="userType">
          <el-select v-model="formInline.userType" placeholder="">
            <el-option
              v-for="item in userList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户查询" prop="userValue">
          <el-input
            v-model="formInline.userValue"
            placeholder="根据“用户信息”输入搜索"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户属性" prop="userAttribute">
          <el-select v-model="formInline.userAttribute" placeholder="">
            <el-option
              v-for="item in userAttList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="展示方式" prop="displayRule">
          <el-select v-model="formInline.displayRule" placeholder="">
            <el-option
              v-for="item in showModeList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="s-btn">
          <el-button
            type="success"
            icon="el-icon-search"
            @click="toSearch('ruleForm')"
          >
            查询
          </el-button>
          <el-button type="success" plain @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { sysNormalDisable } from '@utils/dictionariesFunc'
import { getCreateTime } from '@utils/pubFunction'
export default {
  name: 'VideoWorkTopSearch',
  msg: '小视频管理-作品小视频维护-顶部搜索',
  props: {
    formInline: {
      type: Object,
      default: function() {
        return {
          categoryId: '', // 选择学院
          isWatch: '', // 是否观看
          sortRule: '', // 列表排序
          isDel: '', // 删除状态
          timeType: '', // 创建时间
          displayRule: '', // 展示方式
          userValue: '', // 用户内容查询
          userType: '', // 用户信息
          userAttribute: '', // 用户属性
          startTime: '', // 开始时间
          endTime: '', // 结束时间
          createTime: []
        }
      }
    }
  },
  data() {
    return {
      collegeList: [
        // 学院
        { dictLabel: '全部学院', dictValue: '' }
      ],
      watchList: [
        // 是否观看
        { dictLabel: '全部', dictValue: '' }
      ],
      sortList: [], //列表排序
      delStatusList: [], // 删除状态
      timeTypeList: [{ dictLabel: '创建时间', dictValue: 'createTime' }],
      showModeList: [
        // 展示方式
        { dictLabel: '全部', dictValue: '' }
      ],
      userList: [
        // 用户信息
        { dictLabel: '全部', dictValue: '' }
      ],
      userAttList: [
        // 用户属性
        { dictLabel: '全部', dictValue: '' }
      ],
      rules: {
        sortRule: {},
        isDel: {},
        categoryId: {},
        timeType: {},
        isWatch: {},
        userType: {},
        userValue: {},
        userAttribute: {},
        displayRule: {},
        createTime: { type: 'array' }
      }
    }
  },
  watch: {
    'formInline.createTime': {
      handler(newVal, oldVal) {
        if (newVal && newVal.length > 1) {
          this.formInline.startTime = newVal[0]
          this.formInline.endTime = newVal[1]
        } else {
          this.formInline.startTime = ''
          this.formInline.endTime = ''
        }
      },
      deep: true
    }
  },
  mounted() {
    this.getStatusList()
  },
  methods: {
    /* 搜索 */
    toSearch(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('toSearch')
        } else {
          return false
        }
      })
    },
    /* 重置 */
    reset() {
      this.$refs['ruleForm'].resetFields()
      //设置默认数据
      this.setDefaultValue()
    },
    async getWeekDate() {
      // 获取最近一周时间
      const createTime = getCreateTime()
      this.formInline.createTime = createTime
      this.formInline.startTime = createTime[0]
      this.formInline.endTime = createTime[1]
    },
    /* 获取数据状态 */
    async getStatusList() {
      // 学院
      let data1 = await sysNormalDisable('sys_college', 'collegeType')
      this.collegeList = this.collegeList.concat(data1)
      // 是否观看
      let data2 = await sysNormalDisable('sys_is_watch')
      this.watchList = this.watchList.concat(data2)
      // 删除状态
      this.delStatusList = await sysNormalDisable('sys_is_delete')
      // 展示方式
      let data4 = await sysNormalDisable(
        'video_work_display_rule',
        'displayRule'
      )
      this.showModeList = this.showModeList.concat(data4)
      // 用户信息
      let data5 = await sysNormalDisable('sys_user_search_type')
      this.userList = this.userList.concat(data5)
      // 列表排序
      this.sortList = await sysNormalDisable('video_list_sort')
      // 用户属性
      let data6 = await sysNormalDisable('sys_user_attribute')
      this.userAttList = this.userAttList.concat(data6)
      //设置默认数据
      this.setDefaultValue()
      //触发获取列表
      this.$emit('toSearch')
    },
    /*设置默认数据*/
    setDefaultValue() {
      this.formInline.sortRule = this.sortList[0].dictValue
      this.formInline.isDel = this.delStatusList[0].dictValue
      this.formInline.timeType = 'createTime'
      this.getWeekDate()
    }
  }
}
</script>

<style lang="less">
#VideoWorkTopSearch {
  .el-input {
    &.el-input--suffix {
      width: 182px !important;
    }
  }
  .datetimerange {
    margin-left: -15px;
  }
  label.el-form-item__label {
    width: 70px !important;
  }
}
</style>
